package com.xxd.spring.boot.demo.wubolin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxd.spring.boot.demo.wubolin.Result;
import com.xxd.spring.boot.demo.wubolin.entity.Teacher;
import com.xxd.spring.boot.demo.wubolin.exception.MyException;
import com.xxd.spring.boot.demo.wubolin.mapper.TeacherMapper;
import com.xxd.spring.boot.demo.wubolin.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    public int add(Teacher teacher) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("nickname",teacher.getNickname());
        Teacher dbTea = teacherMapper.selectOne(queryWrapper);
        if(dbTea != null){
            throw new MyException("教师已存在","123");
        }
        int  insert = teacherMapper.insert(teacher);
        return insert;
    }

    @Override
    public int update(Teacher teacher) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("nickname",teacher.getNickname());
        Teacher dbTea = teacherMapper.selectOne(queryWrapper);
        if(dbTea != null){
            throw new MyException("教师不存在","123");
        }
        return teacherMapper.updateById(teacher);
    }

    @Override
    public int delete(Integer id) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Teacher dbTea = teacherMapper.selectOne(queryWrapper);
        if(dbTea != null){
            throw new MyException("教师不存在","123");
        }
        return teacherMapper.deleteById(id);
    }

    @Override
    public IPage<Teacher> listPage(Page page, Map<String, Object> params) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(params);
        Page resPage = teacherMapper.selectPage(page,queryWrapper);
        if (resPage == null || resPage.getSize() == 0){
            throw new MyException("教师不存在","123");
        }
        return resPage;
    }

    @Override
    public List list(Map<String, Object> params) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(params);
        List list = teacherMapper.selectList(queryWrapper);
        if(list == null || list.isEmpty()){
            throw new MyException("教师不存在","123");
        }
        return list;
    }

    @Override
    public Teacher tea(Integer id) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Teacher dbTea = teacherMapper.selectOne(queryWrapper);
        if(dbTea == null){
            throw new MyException("教师不存在","123");
        }
        return teacherMapper.tea(id);
    }

    @Override
    public List<Teacher> all() {
        return teacherMapper.all();
    }
}
